package com.mdd.admin.controller;

import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IDemoService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.common.aop.NotLogin;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.entity.Demo;
import com.mdd.common.util.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("api/demo")
@Api(tags = "测试")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DemoController {

    private final IDemoService demoService;

    @NotLogin
    @ApiOperation(value = "查询列表")
    @GetMapping("/list")
    public AjaxResult list() {
        return AjaxResult.success(demoService.list());
    }

    @NotLogin
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public AjaxResult add (@RequestBody Demo demo) {
        demoService.save(demo);
        return AjaxResult.success(demoService.list());
    }

    @NotLogin
    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public AjaxResult update (@RequestBody Demo demo) {
        Demo entity = demoService.getById(demo.getId());
        entity.setName(demo.getName());
        demoService.updateById(entity);
        return AjaxResult.success(demoService.list());
    }

    @NotLogin
    @ApiOperation(value = "删除")
    @GetMapping("/del")
    public AjaxResult del (Integer id) {
        demoService.removeById(id);
        return AjaxResult.success(demoService.list());
    }

    @NotLogin
    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public void export() throws IOException {
        List<Demo> exportList = demoService.list();
        EasyExcelUtil<Demo> easyExcelUtil = new EasyExcelUtil<>(Demo.class);
        easyExcelUtil.export(exportList);
    }


}
